package com.polaris.live.popup

import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.HowPlayAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupHowPlayBinding
import com.polaris.live.resp.back_resp.GameVo
import com.polaris.live.resp.bean.HowPlayBean
import com.polaris.live.ui.game.GameUtils

/**
 * HowPlayPopup 玩法说明
 * @author Created by 天晴 on 2024/1/19 11:16
 * @since 1.0.0
 **/
class HowPlayPopup(
    context: Context,
    private val list: List<GameVo>,
    private val roomId: String
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupHowPlayBinding

    override fun getImplLayoutId(): Int {
        return R.layout.popup_how_play
    }

    private val mAdapter by lazy { HowPlayAdapter() }

    private var chickIndex = 0

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupHowPlayBinding.bind(popupImplView)
        mBinding.tvAnswerPhone.setOnSingleClickListener {
            dismiss()
            GameUtils.showGameFragment(
                context,
                list[chickIndex],
                roomId = roomId
            )
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItem(position)
            when (view.id) {
                R.id.contect -> {
                    chickIndex = position
                    setSlogan()
                    if (!item.isChecked) {
                        item.isChecked = true
                        mAdapter.data.forEachIndexed { index, howPlayBean ->
                            if (position != index) {
                                howPlayBean.isChecked = false
                            }
                        }
                        mAdapter.notifyDataSetChanged()
                    }
                }
            }
        }
        mBinding.recyclerView.apply {
            addItemDecoration(object : ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    val position = parent.getChildAdapterPosition(view)
                    if (position != 0) {
                        outRect.left = 9.dp
                    }
                }
            })
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = mAdapter
        }
        setSlogan()
        setData(list)
    }

    private fun setSlogan() {
        val data = list[chickIndex]
        mBinding.tvTitle.text = data.slogan
        if (!data.betAmount.isNullOrEmpty()) {
            mBinding.betamountLl.visibility = VISIBLE
            val str = data.betAmount.joinToString("/")
            mBinding.tvBetamount.text = str
        } else {
            mBinding.betamountLl.visibility = GONE
        }
    }

    fun setData(list: List<GameVo>) {
        list.forEachIndexed { index, gameVo ->
            mAdapter.data.add(HowPlayBean(chickIndex == index, gameVo))
        }
        mAdapter.notifyDataSetChanged()
    }

    companion object {

        fun show(context: Context, list: List<GameVo>, roomId: String) {
            XPopup.Builder(context)
                .hasNavigationBar(false)
                .isDestroyOnDismiss(true)
                .asCustom(HowPlayPopup(context, list, roomId))
                .show()
        }
    }
}